package com.zhang.testSegmentFile;

import java.io.*;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author 弯冬水
 * @Date 2023/3/5 0:27
 * @Description: TODO
 * @Version 1.0
 */
public class Test1 {
    public static void main(String[] args) throws Exception {

        extracted();
        merge();
    }

    private static void extracted() throws Exception {
        String path = "D:\\Camera\\2019-02-19-232519922.mp4";
        File file = new File(path);
        String fileName = file.getName();
        System.out.println("fileName = " + fileName);
        String filename = fileName.split("\\.")[0];
        long length = file.length();
//        merge();

        System.out.println("length = " + length);
        BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(path));
        long segmentSize = 2 * 1024 * 1024; //2M
        long count = length % segmentSize == 0 ? length / segmentSize : length / segmentSize + 1;
        byte[] byt = new byte[1024];
        int read;


        for (long i = 0; i < count; i++) {
            int readCount = 0;
            int j = 0;
            int segmentCount = 0;
            BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream("D:\\file\\" + fileName + "." + i));
            ds:
            while ((read = inputStream.read(byt)) != -1) {
                System.out.println("read = " + read);
                j++;

                segmentCount += read;

                if (j == 2048) {
                    outputStream.close();
                    break ds;
                }
                outputStream.write(byt);
                outputStream.flush();
            }
            System.out.println("segmentCount = " + segmentCount);
        }
    }

    private static void merge() throws Exception {
        String path = "D:/file/2019-02-19-232519922.mp4";
        String substring = path.substring(path.lastIndexOf("/") + 1);
        System.out.println("substring = " + substring);
        File file = new File("D:\\file");
        File[] files = file.listFiles();
        ArrayList<File> list = new ArrayList<>();
        for (int i = 0; i < files.length; i++) {
            if (files[i].getPath().contains(substring)) {
                list.add(files[i]);
            }
        }
        for (File file1 : list) {
            System.out.println("file1 = " + file1.getName());
        }

        List<File> collect = list.stream().sorted(Comparator.comparing(filess -> {
            int i = Integer.parseInt(filess.getName().split("\\.")[2]);
            return i;
        })).collect(Collectors.toList());

        BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(path));
        for (File fileOK : collect) {
            byte[] bytes = new byte[1024];

            BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(fileOK));
            while (inputStream.read(bytes) != -1) {
                outputStream.write(bytes);
            }
            inputStream.close();
        }


        outputStream.close();
        System.out.println("合并完成");
        File file1 = new File(path);
        System.out.println("file1 = " + file1.length());
    }
}
